package controller.regist;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.DBException;
import model.InputCheck;
import model.InputException;

@WebServlet("/RegistConfirmation")
public class RegistConfirmation extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String err_msg = "";
		String disp = "/RegistConfirmation.jsp";

		// inputcheckを初期化
		InputCheck inputcheck = new InputCheck(
				request.getParameter("employee_no"),
				request.getParameter("password"),
				request.getParameter("password_again"),
				request.getParameter("first_name"),
				request.getParameter("last_name"), request.getParameter("sex"),
				request.getParameter("join_date"),
				request.getParameter("section"), request.getParameter("mail"));

		// 入力された値をチェック
		try {
			inputcheck.check_null();
			inputcheck.check_txtlength();
			inputcheck.check_passagain();
			inputcheck.check_pass_form();
			inputcheck.check_joindate();
			inputcheck.check_joindate_form();
			inputcheck.check_joindate_form2();
			inputcheck.check_same_empno();

			// エラーコード によって画面変える(入力エラー)
		} catch (InputException e) {
			switch (e.getErrorCode()) {
			case InputException.INVALID_INPUT:
				disp = "/InputErr.jsp";
				break;
			case InputException.BLANK_ID:
				disp = "/InputErr.jsp";
				break;
			case InputException.DUPLICATE_ID:
				disp = "/ErrSameEmployeeNo.jsp";
				break;
			}
			// エラーメッセージを設定(入力エラー)
			err_msg += e.getMessage();
			System.out.println(err_msg);

			// エラーコード によって画面変える(DBエラー)
		} catch (DBException e) {
			disp = "/DBErr.jsp";

			// エラーメッセージを設定(DBエラー)
			err_msg += e.getMessage();
			System.out.println(err_msg);
		}

		// RequestDispatcher
		ServletContext sc = getServletContext();
		RequestDispatcher rd = sc
				.getRequestDispatcher(disp);
		// forward
		rd.forward(request, response);
	}
}